<template>
  <div class="header">
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item
          v-for="(item, index) in breadList"
          :key="index"
          :to="{ path: item.path }"
          >{{ item.title }}</el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <div>
      <ul class="head_ul">
        <li>
          <el-dropdown>
            <span class="el-dropdown-link">
              欢迎，{{ accountInfo.account
              }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item  @click.native="gopath('/users/personal')">个人中心</el-dropdown-item>
              <el-dropdown-item @click.native="gopath('/login')">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </li>
        <li>
          <div class="demo-type">
            <div>
              <el-avatar :src="accountInfo.imgUrl"></el-avatar>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { api_personal } from "../../api/users";
export default {
  data() {
    return {
      breadList: [],
      accountInfo: {},
    };
  },
  mounted() {
    this.getPersonal();
    this.$bus.$on("sendHead", this.setHead);
  },

  methods: {
    gopath(path){
      this.$router.push(path);
    },
    setHead(url) {
      this.accountInfo.imgUrl = url;
      //将更改的数据重新出入到本地
      this.$session.set("accountInfo", this.accountInfo);
    },
    async getPersonal() {
      let rst = await api_personal();
      this.accountInfo = rst.accountInfo;
      this.$session.set("accountInfo", rst.accountInfo);
      console.log(rst);
    },
  },
  watch: {
    $route: {
      deep: true,
      immediate: true,
      handler() {
        let arr = [];
        this.$route.matched.forEach((item) => {
          if (item.meta.title && item.meta.title != "首页") {
            arr.push({ path: item.path, title: item.meta.title });
          }
        });
        this.breadList = arr;
        console.log(arr);
        console.log(this.$route);
      },
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.head_ul {
  display: flex;
  align-items: center;
}
</style>
